package com.wwfish.cmsui.modules.common.client.widget.xmlform;

import com.nexustar.gwt.widgets.client.ui.form.*;
import com.nexustar.gwt.widgets.client.ui.textbox.*;
import com.wwfish.cmsui.dashboard.client.CMSDashboard;
import com.wwfish.gwt.reflection.client.ModelReflection;
import com.wwfish.gwt.reflection.client.ReflectionHelper;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-4
 * Time: 15:06:48
 * To change this template use File | Settings | File Templates.
 */
public class FormElementCreator {
    public static FormElement getFormElement(final XMLFormContainer.ElementMetaData meta, final IFormProvider provider) {
        Object[] parameter = null;
        if (meta.getParameters() != null && meta.getParameters().size() > 0) {
            parameter = new Object[meta.getParameters().size() + 1];
            parameter[0] = meta.getLabelText();
            int i = 1;
            for (Iterator it = meta.getParameters().iterator(); it.hasNext();) {
                parameter[i++] = it.next();
            }
        } else {
            parameter = new Object[1];
            parameter[0] = meta.getLabelText();
        }
        final FishLabelWidget ui = getFishLabelWidget(meta.getLabelType(), parameter);
        FormElement formElement = new FormElement(ui, new IFormElementProvider() {
            public void setValue(Object object) {
                ui.setValue(ModelReflection.getProperty(object, meta.getProperty()));
                if (provider != null) {
                    provider.setValue(meta.getId(), object);
                }
            }

            public void getValue(Object object) {
                ModelReflection.setProperty(object, meta.getProperty(), ReflectionHelper.translator(ui.getValue(), meta.getTranslator()));
                if (provider != null) {
                    provider.getValue(meta.getId(), object);
                }
            }

            public void reset(IFormElement element) {
                FishLabelWidget w = (FishLabelWidget) element.getWidget();
                w.reset();
                w.removeErrorMessage();
                if (provider != null) {
                    provider.reset(meta.getId(), (FormElement) element);
                }

            }
        });
        //init validator begin
        IValidator[] vas;
        int size = 0;
        String[] validators = null;
        IValidator[] ivas = null;
        if (meta.getValidator() != null) {
            validators = meta.getValidator().split(",");
            size = validators.length;
        }
        if (provider != null && provider.getValidator() != null && provider.getValidator().length > 0) {
            ivas = provider.getValidator();
            size += provider.getValidator().length;
        }
        vas = new IValidator[size];
        int i = 0;
        if (validators != null) {
            for (int j = 0; j < validators.length; j++) {
                vas[i++] = getValidator(validators[j]);
            }
        }
        if (ivas != null && ivas.length > 0) {
            for (int j = 0; j < ivas.length; j++) {
                vas[i++] = ivas[j];
            }
        }
        formElement.setValidators(vas);
        //init validator end


        if (provider != null) {
            provider.init(meta.getId(), formElement);
        }
        return formElement;
    }

    public static IValidator getValidator(String type) {
        if (type.equals("require")) {
            return ValidatorCreator.require();
        } else if (type.equals("number")) {
            return ValidatorCreator.isNumber();
        } else if (type.equals("password")) {
            return ValidatorCreator.isPassword();
        } else if (type.equals("telephone")) {
            return ValidatorCreator.isTelephone();
        } else if (type.equals("email")) {
            return ValidatorCreator.isEmail();
        } else if (type.equals("float")) {
            return ValidatorCreator.isFloat();
        } else if (type.equals("word")) {
            return ValidatorCreator.isWord();
        } else
            throw new IllegalArgumentException("no match Validator!type:" + type);
    }


    private static FishLabelWidget getFishLabelWidget(String type, Object[] parameters) {
        if (type.equals(FishLabelTextBox.class.getName())) {
            return new FishLabelTextBox((String) parameters[0]);
        } else if (type.equals(FishLabelCheckBox.class.getName())) {
            return new FishLabelCheckBox((String) parameters[0]);
        } else if (type.equals(FishLabelDateBox.class.getName())) {
            return new FishLabelDateBox((String) parameters[0], Boolean.valueOf((String) parameters[1]));
        } else if (type.equals(FishLabelImageBox.class.getName())) {
            if (parameters.length == 3)
                return new FishLabelImageBox((String) parameters[0], (String) parameters[1], (String) parameters[2]);
            else
                return new FishLabelImageBox((String) parameters[0], CMSDashboard._UPLOAD_IMAGE_PATH, CMSDashboard._DOWN_IMAGE_PATH);
        } else if (type.equals(FishLabelListBox.class.getName())) {
            return new FishLabelListBox((String) parameters[0]);
        } else if (type.equals(FishLabelListTextBox.class.getName())) {
            return new FishLabelListTextBox((String) parameters[0]);
        } else if (type.equals(FishLabelMultiImageBox.class.getName())) {
            if (parameters.length == 3)
                return new FishLabelMultiImageBox((String) parameters[0], (String) parameters[1], (String) parameters[2]);
            else
                return new FishLabelMultiImageBox((String) parameters[0], CMSDashboard._UPLOAD_IMAGE_PATH, CMSDashboard._DOWN_IMAGE_PATH);
        } else if (type.equals(FishLabelMultiListBox.class.getName())) {
            return new FishLabelMultiListBox((String) parameters[0]);
        } else if (type.equals(FishLabelPasswordBox.class.getName())) {
            return new FishLabelPasswordBox((String) parameters[0]);
        } else if (type.equals(FishLabelSuggestBox.class.getName())) {
            return new FishLabelSuggestBox((String) parameters[0]);
        } else if (type.equals(FishLabelTextAreaBox.class.getName())) {
            return new FishLabelTextAreaBox((String) parameters[0]);
        } else if (type.equals(FishLabelUploadBox.class.getName())) {
            if (parameters.length == 2)
                return new FishLabelUploadBox((String) parameters[0], (String) parameters[1]);
            else
                return new FishLabelUploadBox((String) parameters[0], CMSDashboard._UPLOAD_IMAGE_PATH);
        } else
            throw new IllegalArgumentException("no match! type:" + type);
    }
}
